#include <stdio.h>
#include <string.h>

/*chutf8 is from E4B880 to E9BEA5
 *filter all english words and sigbols
 */
char * ch_utf8(char *dst, int dst_len, const char *src)
{
	int ret = 0;
	int i = 0;
	int j = 0;
	int src_len = strlen(src);
	unsigned char k = 0;
	unsigned char k1 = 0;
	unsigned char k2 = 0;
	for(i=0,j=0; i<src_len && j<dst_len; ){
		k = (unsigned char)(src[i]);
		if(k<0x80){			//0xxxxxxx
			i ++;
			continue;
		}else if(k>=0xc0 && k<0xe0){	//110xxxxx 10xxxxxx 
			i += 2;
			continue;
		}else if(k>=0xe0 && k<0xf0){	//1110xxxx 10xxxxxx 10xxxxxx 
			k1 = (unsigned char)(src[i+1]);
			k2 = (unsigned char)(src[i+2]);
			if(k<0xe4 || k>0xe9){
				i += 3;
				continue;
			}else if(k==0xe4){
				if(k1<0xb8){
					i += 3;
					continue;
				}
				if(k1==0xb8 && k2<0x80){
					i += 3;
					continue;
				}
			}else if(k==0xe9){
				if(k1>0xbe){
					i += 3;
					continue;
				}
				if(k1==0xbe && k2>0xa5){
					i += 3;
					continue;
				}
			}
			if(j+3 > dst_len){
				break;
			}
			memcpy(dst+j,src+i,3);
			printf("%s\n",dst);
			i +=3;
			j +=3;
		}else{
			break;
		}
	}
	if(i<src_len && j<dst_len-3){
	}else if(i<src_len && j>=dst_len-3){
	}
	dst[j] = 0;
	return dst;
}

int printx(char * str)
{
	printf("<\n");
	int i = 0;
	for(i=0; i<strlen(str); i++){
		printf("%x", (unsigned char)str[i]);
		if((i+1)%16 == 0){
			printf("\n");
		}else if((i+1)%4 == 0){
			printf("\t");
		}else{
			printf(" ");
		}
	}
	printf("\n>\n");

	return 0;
}

int main(int argc, char ** argv)
{
	char src[1024];
	strncpy(src,argv[1],1024);
	printf("src: %s\n", src);
	char dst[1024*5] = {0};
	ch_utf8(dst,1024,src);
	printx(src);
	printx(dst);
	printf("%s\n", dst);
	return 0;
}

